//
//  shopMainView.m
//  ILiangCang
//
//  Created by ZX on 16-4-16.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "shopMainView.h"
#import "httpEngine.h"
#import "listModel.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "firstCell.h"
#import "secondCell.h"
#import "thirdCell.h"
#import "MJRefresh.h"
#import "Masonry.h"


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height


@implementation shopMainView


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        
        _scrollArray = [[NSMutableArray alloc]initWithCapacity:0];
        _otherArray = [[NSMutableArray alloc]initWithCapacity:0];
        
        
        [self addTheTableView];
    }

    return self;
}

-(void)addTheTableView
{
    
    [httpEngine requestToGetShopMainMessageCompletion:^(NSMutableArray *scrollArray, NSMutableArray *otherArray, NSError *error) {
        _scrollArray = scrollArray;
        _otherArray = otherArray;
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-49-20) style:UITableViewStylePlain];
    
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[firstCell class] forCellReuseIdentifier:@"cell1"];
        [_tableView registerNib:[UINib nibWithNibName:@"secondCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
        [_tableView registerClass:[thirdCell class] forCellReuseIdentifier:@"cell3"];
        [self addSubview:_tableView];
        
        [_tableView addHeaderWithTarget:self action:@selector(headerRefreshing)];
        
        [_tableView reloadData];
    }];

    
    
    
}
-(void)headerRefreshing
{
    [httpEngine requestToGetShopMainMessageCompletion:^(NSMutableArray *scrollArray, NSMutableArray *otherArray, NSError *error) {
        _scrollArray = scrollArray;
        _otherArray = otherArray;
    }];
    [_tableView headerEndRefreshing];
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    listModel * list = _otherArray[indexPath.row];
    
    if (indexPath.row == 0) {
        secondCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        
        for (int i = 0; i<4; i++)
        {
            UIButton * btn =(UIButton *) [cell viewWithTag:i+10];
           listModel * list = _otherArray[i];
           [btn sd_setBackgroundImageWithURL:list.picURL forState:UIControlStateNormal];
            
        }
        
        
        return cell;

    }if (indexPath.row == 1) {
        firstCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        listModel * list = _otherArray[4];
        [cell.longBG sd_setImageWithURL:list.picURL];
        
        return cell;

    }else
    {
        thirdCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
        for (int i = 0; i<2; i++)
        {
            UIButton * btn =(UIButton *) [cell viewWithTag:i+20];
            listModel * list = _otherArray[i+5];
            [btn sd_setBackgroundImageWithURL:list.picURL forState:UIControlStateNormal];
            
        }

        
        
        return cell;

    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 200;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 360;
    }
    if (indexPath.row == 1)
    {
        return 180;
    }else
    {
        return 160;
    }
    
}

-(UIView * )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    _speed = WIDTH;
    UIView * view = [[UIView alloc]init];
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    scrollView.contentSize = CGSizeMake(3*WIDTH, 200);
    scrollView.pagingEnabled = YES;
    for (int i = 0; i<3; i++)
    {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(i*WIDTH, 0, WIDTH, 200);
        listModel * list = _scrollArray[i];
        
        [btn sd_setBackgroundImageWithURL:list.picURL forState:UIControlStateNormal];
        
        [scrollView addSubview:btn];
        
    }
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    [view addSubview:scrollView];

    pageControl = [[UIPageControl alloc]init];
    pageControl.numberOfPages = 3;
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    [view addSubview:pageControl];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view.mas_centerX);
        make.bottom.mas_equalTo(view.mas_bottom).with.offset(0);
    }];

    
    
    if (!_timer) {
        _timer =
        [NSTimer scheduledTimerWithTimeInterval:1.6 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    }
    
    
    return view;
}
//pageControl的方法实现
-(void)pageControlValueChanged:(UIPageControl* )pageControl
{
    //根据pageControl的当前页数改变scrollView的偏移量
    [scrollView setContentOffset:CGPointMake(WIDTH*pageControl.currentPage,0) animated:YES];
}
//定时器方法的实现
-(void)onTimer
{
    if (scrollView.contentOffset.x >= WIDTH*2)
    {
        _speed = -WIDTH;
    }
    if (scrollView.contentOffset.x <= 0) {
        _speed = WIDTH;
    }
    //在原来的基础上每次让scrollView的x偏移量增加_speed
    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x + _speed, 0) animated:YES];
}
#pragma mark ---UIScrollViewDelegate-----

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //根据scrollView的偏移量改变pageControl的当前页数
    pageControl.currentPage = scrollView.contentOffset.x/WIDTH;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    
    
    CGFloat sectionHeaderHeight = 200;
    
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
}




@end
